//
//  DHLoginViewController.m
//  DHKuaiYi
//
//  Created by Cedric Zheng on 15/11/2016.
//  Copyright © 2016 30-陈辉华. All rights reserved.
//

#import "DHLoginViewController.h"


@interface DHLoginViewController ()

@end

@implementation DHLoginViewController

#pragma mark - Life Cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view setBackgroundColor:[UIColor blueColor]];
    
    /*----设置导航栏----*/
    self.title = @"登陆";
    
}

- (void)xhhSetupSubviews {
    [super xhhSetupSubviews];
    
    /*----设置用户名栏----*/
    [self.userNameTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.centerY.equalTo(self.view).offset(-100.0);
        make.width.mas_equalTo(314.0);
    }];
    
    /*----设置密码栏----*/
    [self.passwordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.userNameTextField);
        make.top.equalTo(self.userNameTextField.mas_bottom).offset(10.0);
    }];
    
    /*----设置确认登陆按钮----*/
    //把父类的绿色按钮当登陆按钮使用
    UIButton *loginButton = self.aGreenButton;
    [loginButton setTitle:@"登录" forState:UIControlStateNormal];
    [loginButton addTarget:self action:@selector(loginButtonDidTapped) forControlEvents:UIControlEventTouchUpInside];
    
    [loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.userNameTextField);
        make.top.equalTo(self.passwordTextField.mas_bottom).offset(50.0);
    }];
    
    /*----注册按钮----*/
    UIButton *registerButton = [[UIButton alloc] init];
    [registerButton setTitle:@"注册账号" forState:UIControlStateNormal];
    [registerButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [self.view addSubview:registerButton];
    registerButton.titleLabel.font = [UIFont systemFontOfSize:14.0];
    [registerButton addTarget:self action:@selector(registerButtonDidTapped) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:registerButton];
    [registerButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(loginButton.mas_bottom).offset(20.0);
    }];
    
}

#pragma mark - Events
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    //退出键盘
    [self.view endEditing:YES];
}

/**
 点击注册账号按钮
 */
- (void)registerButtonDidTapped {
    [self.navigationController pushViewController:[self getControllerWithClassName:@"DHRegisterViewController"] animated:YES];
}

- (void)loginButtonDidTapped {
    //1,获取到数据库账号和密码
    NSArray *accountArr = [XHHUserDefaultsTool objetForKey:USER_ACCOUNT_KEY];
    
    //2,遍历用户数组 匹配账号登录
    for (NSDictionary *dict in accountArr) {
        
        NSString *userID = dict[@"user"];
        NSString *password = dict[@"password"];
        
        if ([userID isEqualToString:self.userNameTextField.text] && [password isEqualToString:self.passwordTextField.text]) {
            
            //获得当前登陆账号
            if (![XHHUserDefaultsTool objetForKey:CURRENT_LOGIN_ACCOUNT_KEY]) {
                NSMutableArray *accountInfo = [[NSMutableArray alloc] init];
                [XHHUserDefaultsTool saveObject:accountInfo forKey:CURRENT_LOGIN_ACCOUNT_KEY];
            }
            
            NSDictionary *accountDict = @{@"user":self.userNameTextField.text};
            NSMutableArray* newAccountInfo = [NSMutableArray arrayWithArray:[XHHUserDefaultsTool objetForKey:CURRENT_LOGIN_ACCOUNT_KEY]];
            [newAccountInfo addObject:accountDict];
            [XHHUserDefaultsTool saveObject:newAccountInfo forKey:CURRENT_LOGIN_ACCOUNT_KEY];
            
            [SVProgressHUD setDefaultStyle:SVProgressHUDStyleLight];
            [SVProgressHUD showSuccessWithStatus:@"登陆成功！"];
            delay(1.5);
            
            //发送一个登陆状态发生改变的通知
            [[NSNotificationCenter defaultCenter] postNotificationName:LOGIN_STATUS_CHANGED object:self];

            //移除当前控制器
            [self.view endEditing:YES];
            [self dismissViewControllerAnimated:YES completion:nil];
            
            return;
        }
    }
    
    [SVProgressHUD showErrorWithStatus:@"账号密码错误"];
    delay(1.5);
}

#pragma mark – Private Methods

#pragma mark - <#Delegates#>

#pragma mark - <#Datasources#>

#pragma mark – Getters and Setters

@end
